package jchat.genral;

import java.io.UnsupportedEncodingException;

import jchat.interfaces.IAppManager;
import jchat.services.IMService;

import org.json.JSONObject;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import chat.jdaas.R;

public class Showfriend  extends Activity{
	 private String data;
	    private ReadContact objcontact;
	    JSONObject objjson;
	    private IAppManager imService;
	   
	   public String contactlist=null;
	   private ServiceConnection mConnection = new ServiceConnection() {
	        

			public void onServiceConnected(ComponentName className, IBinder service) {
	            // This is called when the connection with the service has been
	            // established, giving us the service object we can use to
	            // interact with the service.  Because we have bound to a explicit
	            // service that we know is running in our own process, we can
	            // cast its IBinder to a concrete class and directly access it.
	            imService = ((IMService.IMBinder)service).getService();  
	            
	            
	        }

	        public void onServiceDisconnected(ComponentName className) {
	            // This is called when the connection with the service has been
	            // unexpectedly disconnected -- that is, its process crashed.
	            // Because it is running in our same process, we should never
	            // see this happen.
	        	imService = null;
	            Toast.makeText(Showfriend.this, R.string.local_service_stopped,
	                    Toast.LENGTH_SHORT).show();
	        }
	    };

	@Override
	protected void onCreate(Bundle saveinstance)
	{
		super.onCreate(saveinstance);
		setContentView(R.layout.allcontactlist);
		Button btn=(Button)findViewById(R.id.selectimage);
		   objcontact=new ReadContact(this);
		      objjson=new JSONObject();
		     // objjson=objcontact.getcontects();
		      data=objjson.toString();
		      Toast.makeText(getApplicationContext(), data.toString(), Toast.LENGTH_LONG).show();
		      btn.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					if (imService == null) {
						Toast.makeText(getApplicationContext(),R.string.not_connected_to_service, Toast.LENGTH_LONG).show();
						//showDialog(NOT_CONNECTED_TO_SERVICE);
						return;
					}
					else if (imService.isNetworkConnected() == false)
					{
						Toast.makeText(getApplicationContext(),R.string.not_connected_to_network, Toast.LENGTH_LONG).show();
						//showDialog(NOT_CONNECTED_TO_NETWORK);
						
					}
				
						
						Thread loginThread = new Thread(){
							String result = new String();
							private Handler handler = new Handler();
							@Override
							public void run() {
								
								
								result=imService.sendcontectlist(data);
								handler.post(new Runnable(){
									
									public void run() {
										Toast.makeText(getApplicationContext(), result, Toast.LENGTH_LONG).show();
											
									}

								});
							}
							
						};
						loginThread.start();
						
						}
								
				
				
			});
		     
	}
	
	@Override
	protected void onResume() {
		bindService(new Intent(Showfriend.this, IMService.class), mConnection , Context.BIND_AUTO_CREATE);
		   
		super.onResume();
	}
	
	@Override
	protected void onPause() 
	{
		unbindService(mConnection);
		super.onPause();
	}
	

}
